const mongoose = require('mongoose')
const ChatMsgSchema = mongoose.model('ChatMsgSchema')
const Router = require('koa-router')
const ChatRouter = new Router()
ChatRouter.get('/getChatList', async ctx => {
  try {
    // const { fromUserId, toUserId } = ctx.request.query
    // const chatList = await ChatMsgSchema.find({fromUserId: {$in: [fromUserId, toUserId]}, toUserId: {$in: [fromUserId, toUserId]}}).exec()
    const chatList = await ChatMsgSchema.find().exec()
    ctx.body = {
      code: 200,
      data: chatList || [],
      msg: '查询成功'
    }
  } catch (error) {
    ctx.body = {
      code: 500,
      msg: '查询失败'
    }
  }
})
ChatRouter.post('/saveChat', async ctx => {
  try {
    const { body } = ctx.request
    await new ChatMsgSchema(body).save()
    ctx.body = {
      code: 200,
      msg: '保存成功'
    }
  } catch (error) {
    ctx.body = {
      code: 500,
      msg: '保存失败'
    }
  }
})
module.exports = ChatRouter